<?php
/**
 * Created by PhpStorm.
 * User: 顾明轩
 * Date: 2017/10/9
 * Time: 9:56
 */

namespace Admin\Controller;

use Common\Model\Alipay;
use Think\Controller;

class OrderController extends BaseController
{
    public function order_list()
    {
//        0=处理中,1=已完成,2=已取消
        $where['state'] = 1;
        $where['supplier_id'] = session('supplier_id');
        switch (I('type')) {
            case 'clz':
                $where['order_state'] = 0;
                break;
            case 'ywc':
                $where['order_state'] = 1;
                break;
            default:
                $where['state'] = 1;
                $where['supplier_id'] = session('supplier_id');
        }
        if (I('o_n') != "") {
            $user_info = M('user_list')->where('username=' . I('o_n'))->find();
            if ($user_info) {
                $where['user_id'] = $user_info['user_id'];
            } else {
                $where['order_sn'] = I('o_n');
            }
//            $where = $where." and (order_no='".I('o_n')."' ".($user_info?" or user_id=".$user_info['id']:"").")";
        }

        $count = M('order_list')->where($where)->count();
        $Page = getpage($count, 10);
        $limit = $Page->firstRow . ',' . $Page->listRows;
        $order_list = M('order_list')->where($where)->limit($limit)->select();
        foreach ($order_list as $k => $v) {
            $user_info = M('user_list')->where('user_id=' . $v['user_id'])->find();
            $order_list[$k]['user_ids'] = $user_info['username'];
            $order_list[$k]['order_types'] = $v['order_type'] == 0 ? '商城订单' : '积分商城订单';
            $goods_info = M($v['order_type'] == 0 ? 'goods_list' : 'integralmall_list')->
            where($v['order_type'] == 0 ? 'goods_id=' : 'integral_goods_id=' . $v['goods_id'])->find();
            $order_list[$k]['goods_names'] = $goods_info['goods_name'];
            $order_list[$k]['payment_prices'] = $v['payment_price'];
            $address = M("user_address")->where("address_id=" . $v['address_id'])->find();
            $order_list[$k]['address_ids'] = $address['address'];
            $order_list[$k]['goods_nums'] = $v['goods_num'];
        }

        $this->assign('order_list', $order_list);
        $this->assign('type', I('type'));
        $this->assign('o_n', I('o_n'));
        $this->assign('page', $Page->show());
        $this->display();

    }

    public function order_info()
    {
        $arr['order_id'] = I('id');
        $order_list = M('order_list')->where($arr)->select();
        foreach ($order_list as $k => $v) {
            $user_info = M('user_list')->where('user_id=' . $v['user_id'])->find();
            $order_list[$k]['user_ids'] = $user_info['username'];
            $order_list[$k]['order_types'] = $v['order_type'] == 0 ? '商城订单' : '积分商城订单';
            $goods_info = M($v['order_type'] == 0 ? 'goods_list' : 'integralmall_list')->
            where($v['order_type'] == 0 ? 'goods_id=' : 'integral_goods_id=' . $v['goods_id'])->find();
            $order_list[$k]['goods_names'] = $goods_info['goods_name'];
            $order_list[$k]['payment_prices'] = $v['payment_price'];
            $address = M("user_address")->where("address_id=" . $v['address_id'])->find();
            $order_list[$k]['address_ids'] = $address['address'];
            $order_list[$k]['goods_nums'] = $v['goods_num'];
        }

        $this->assign('order_lists', $order_list);
        $this->display();
    }

    public function achieve()
    {
        $id = I('post.id');
        $data['order_state'] = 1;
        $res = M('order_list')->where('order_id=' . $id)->save($data);
        if ($res != false) {
            $arr['info'] = '完成，修改成功！';
            $arr['state'] = 1;
            $this->ajaxReturn($arr, 'json');
        } else {
            $arr['info'] = '完成，修改失败！';
            $arr['state'] = 0;
            $this->ajaxReturn($arr, 'json');
        }
    }

    public function abandon()
    {
        $id = I('post.id');
        $data['supplier_id'] = 0;
        $res = M('order_list')->where('order_id=' . $id)->save($data);
        if ($res != false) {
            $arr['info'] = '放弃，修改成功！';
            $arr['state'] = 1;
            $this->ajaxReturn($arr, 'json');
        } else {
            $arr['info'] = '放弃，修改失败！';
            $arr['state'] = 0;
            $this->ajaxReturn($arr, 'json');
        }
    }
//    public function upload_order(){
//        if(IS_AJAX) {
//            if (I('type') && I('type') != "" && I('o_id') && I('o_id') != "" && I('o_n') && I('o_n') != "" && I('v') && I('v') != "") {
//                $where = array(
//                    'order_id' => I('o_id'),
//                    'order_no' => I('o_n'),
//                    'order_state' => 3
//                );
//                $vl = str_replace('@$nbsp;@', '&nbsp;', I('v'));
//                switch (I('type')) {
//                    case 'logistics':
//                        $is_true = $this->logistics($where, $vl);
//                        break;
//                    case 'waybill':
//                        $is_true = $this->waybill($where, $vl);
//                        break;
//                    case 'addres':
//                        $is_true = $this->addres($where, $vl);
//                        break;
//                    case 'order_price':
//                        $is_true = $this->order_price($where, $vl);
//                        break;
//                    case 'freight':
//                        $is_true = $this->freight($where, $vl);
//                        break;
//                }
//                $data = $is_true == true ? genBody(1, '保存成功') : genBody(0, '保存失败');
//            } else {
//                $data = genBody(0, '保存失败');
//            }
//            $this->ajaxReturn($data, 'JSON');
//        }else{
//            echo "非法访问";
//        }
//    }

//    private function logistics($where,$vl){
//        $where['order_state']=3;
//        return (M('order_list')->where($where)->save(array('logistics'=>$vl)))?true:false;
//    }
//    private function waybill($where,$vl){
//        $where['order_state']=3;
//        return (M('order_list')->where($where)->save(array('waybill'=>$vl)))?true:false;
//    }
//    private function addres($where,$vl){
//        $where = "order_id=".$where['order_id']." and order_no=".$where['order_no']." and (order_state=1 or order_state=2)";
//        return (M('order_list')->where($where)->save(array('addres'=>$vl)))?true:false;
//    }
//    private function order_price($where,$vl){
//        $where['order_state']=1;
//        return (M('order_list')->where($where)->save(array('order_price'=>$vl)))?true:false;
//    }
//    private function freight($where,$vl){
//        $where['order_state']=1;
//        return (M('order_list')->where($where)->save(array('freight'=>$vl)))?true:false;
//    }
//
//
//    public function deliver_goods(){
//        if(IS_AJAX) {
//            $logistics = I('logistics');
//            $waybill = I('waybill');
//            if ($logistics != "" && $waybill != "") {
//                $where = array(
//                    'order_state' => 2,
//                    'order_id' => I('o_id'),
//                    'order_no' => I('o_n')
//                );
//                $data = (M('order_list')->where($where)->save(array('logistics' => $logistics, 'waybill' => $waybill,'order_state'=>3))) ? genBody(1, '保存成功') : genBody(0, '保存失败');
//            } else {
//                $data = genBody(0, '保存失败');
//            }
//            $this->ajaxReturn($data, 'JSON');
//        }else{
//            echo "非法访问";
//        }
//    }
//
//    public function refund(){
//        if(IS_AJAX) {
//            $refund_amount = I('r_a');
//            $refund_reason = I('r_r');
//            $admin_id = $_SESSION['admin_id'];
//            if ($refund_amount != "" && $refund_reason != "") {
//                $where = array(
//                    'order_state' => 5,
//                    'order_id' => I('o_id'),
//                    'order_no' => I('o_n')
//                );
//                $save = array(
//                    'refund_amount' => $refund_amount,
//                    'refund_reason' => $refund_reason,
//                    'order_state'=>6,
//                    'admin_id'=>$admin_id
//                );
//                if(M('order_list')->where($where)->save($save)){
//                    $Alipay = new Alipay();
//                    if($Alipay->refund($where['order_no'],$refund_amount,$refund_reason,"TK".$where['order_no'],$admin_id)==true){
//                        $data = genBody(1, '退款成功');
//                    }else{
//                        $where = array(
//                            'order_state' => 6,
//                            'order_id' => I('o_id'),
//                            'order_no' => I('o_n')
//                        );
//                        M('order_list')->where($where)->save(array('order_state'=>5));
//                        $data = genBody(0, '退款失败');
//                    }
//                }
//            } else {
//                $data = genBody(0, '退款失败');
//            }
//            $this->ajaxReturn($data, 'JSON');
//        }else{
//            echo "非法访问";
//        }
//    }
}